﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace KaixinMobile.Forms
{
    using Kaixin.House.Garden;

    public partial class frmAntiTheive : Form
    {
        private AntiTheive _Anti;

        public frmAntiTheive(AntiTheive anti)
        {
            InitializeComponent();

            _Anti = anti;
        }

        private void SetQuestion(byte[] data)
        {
            try
            {
                if (data != null)
                {
                    MemoryStream ms = new MemoryStream(data);
                    pbQuestion.Image = new Bitmap(ms);
                }
            }
            catch (Exception)
            {
            }
        }

        private void SetContent(ArrayList cont)
        {
            try
            {
                lvContent.DataSource = cont;
                lvContent.DisplayMember = "Name";
                lvContent.ValueMember = "SeedId";
            }
            catch (Exception)
            {
            }
        }

        public void DoAntiTheive()
        {
            if (_Anti != null && _Anti.Triggered)
            {
                ArrayList cont = _Anti.GetContent();
                if (cont != null)
                    SetContent(cont);
                SetQuestion(_Anti.GetQuestion());
                ShowDialog();
            }
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            if (_Anti != null)
            {
                ArrayList cont = (ArrayList)lvContent.DataSource;
                string ans = "";
                for (int i = 0; i < cont.Count; i++)
                {
                    ans += "," + ((Seed)cont[i]).SeedId;
                }
                if (_Anti.SendAnswer(ans))
                {
                    MessageBox.Show("OK");
                    DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("Fail");
                    DialogResult = DialogResult.Cancel;
                }
            }
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void btnUp_Click(object sender, EventArgs e)
        {
            try
            {
                int index = lvContent.SelectedIndex;
                if (index != 0)
                {
                    ArrayList cont = (ArrayList)lvContent.DataSource;
                    object item = cont[index];
                    cont.RemoveAt(index);
                    cont.Insert(index - 1, item);
                    lvContent.DataSource = null;
                    lvContent.Items.Clear();
                    SetContent(cont);
                    lvContent.SelectedIndex = index - 1;
                }
            }
            catch (Exception)
            {

            }
        }

        private void btnDown_Click(object sender, EventArgs e)
        {
            try
            {
                int index = lvContent.SelectedIndex;
                int maxIndex = this.lvContent.Items.Count - 1;
                if (index != maxIndex)
                {
                    ArrayList cont = (ArrayList)lvContent.DataSource;
                    object item = cont[index];
                    cont.RemoveAt(index);
                    cont.Insert(index + 1, item);
                    lvContent.DataSource = null;
                    lvContent.Items.Clear();
                    SetContent(cont);
                    lvContent.SelectedIndex = index + 1;
                }
            }
            catch (Exception)
            {
            }
        }

        private void frmAntiTheive_Closed(object sender, EventArgs e)
        {
            //if (_Anti != null)
            //    _Anti.Reset();
        }
    }
}